﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.IO;

namespace Super.Common
{
    public class JSONHelper
    {
        public string DataTable2Json(DataTable dt)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                StringBuilder jsonBuilder = new StringBuilder();
                jsonBuilder.Append("{");
                jsonBuilder.Append("ds");
                jsonBuilder.Append(":[");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    jsonBuilder.Append("{");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        //jsonBuilder.Append("\"");
                        jsonBuilder.Append(dt.Columns[j].ColumnName);
                        jsonBuilder.Append(":'");
                        jsonBuilder.Append(dt.Rows[i][j].ToString());
                        jsonBuilder.Append("',");
                    }
                    jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                    jsonBuilder.Append("},");
                }
                jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                jsonBuilder.Append("]");
                jsonBuilder.Append("}");
                return jsonBuilder.ToString();
            }
            else
            {
                return "{ds:{}}";
            }
        }

        public string DataTable2Json(DataTable dt, int pagecount, int count)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                StringBuilder jsonBuilder = new StringBuilder();

                jsonBuilder.Append("{\"pagecount\":\"" + pagecount + "\",\"count\":\"" + count + "\",\"");

                jsonBuilder.Append(dt.TableName);
                jsonBuilder.Append("\":[");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    jsonBuilder.Append("{");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        jsonBuilder.Append("\"");
                        jsonBuilder.Append(dt.Columns[j].ColumnName);
                        jsonBuilder.Append("\":\"");
                        jsonBuilder.Append(dt.Rows[i][j].ToString());
                        jsonBuilder.Append("\",");
                    }
                    jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                    jsonBuilder.Append("},");
                }
                jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                jsonBuilder.Append("]");
                jsonBuilder.Append("}");
                return jsonBuilder.ToString();
            }
            else
            {
                return "{pagecount:0,count:0,ds:{}}";
            }
        }

        public string ObjectToJson<T>(string jsonName, IList<T> t)
        {
            StringBuilder Json = new StringBuilder();
            Json.Append("{\"" + jsonName + "\":[");
            if (t.Count > 0)
            {
                for (int i = 0; i < t.Count; i++)
                {
                    T obj = Activator.CreateInstance<T>();
                    Type type = obj.GetType();
                    PropertyInfo[] pis = type.GetProperties();
                    Json.Append("{");
                    for (int j = 0; j < pis.Length; j++)
                    {
                        Json.Append("\"" + pis[j].Name.ToString() + "\":\"" + pis[j].GetValue(t[i], null) + "\"");
                        if (j < pis.Length - 1)
                        {
                            Json.Append(",");
                        }
                        else
                        {
                            Json.Append("}");
                        }
                        if (i < t.Count - 1)
                        {
                            Json.Append(",");
                        }
                    }
                }
            }
            Json.Append("]}");
            return Json.ToString();
        }

        #region "将可序列的类转化Json数据格式； by 赵国伟；2009-10-15"
        /// <summary>
        /// 将可序列的类转化Json数据格式；[采用.net3.5自带的json支持类]
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ObjectToJson<T>(T obj)
        {
            DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream();
            ds.WriteObject(ms, obj);
            string strJSON = Encoding.UTF8.GetString(ms.ToArray());
            ms.Close();
            return strJSON;
        }

        #endregion

        #region "将指定的Json字符串转化为指定的实体类； by 赵国伟 2009-10-15"
        /// <summary>
        /// 将指定的Json字符串转化为指定的实体类；[采用.net3.5自带的json支持类]
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sJson"></param>
        /// <returns></returns>
        public static T JsonStringToObject<T>(string sJson) where T : class
        {
            DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(sJson));
            T obj = (T)ds.ReadObject(ms);

            ms.Close();
            return obj;
        }

        #endregion

    }
}
